import requests

from bs4 import BeautifulSoup
import lxml

path="E:\photo.txt"

def getUrl(url):
    try:
        r=requests.get(url)
        r.encoding=r.apparent_encoding
        r.raise_for_status()
        return r.text
    except:
        print("获取页面失败！")

def getphotos(html,listInfo):
    soup=BeautifulSoup(html,'lxml')
    imgs=soup.find_all('img')
    print(imgs)
    for img in imgs:
        try:
            t=[img.attrs['data-original']]
            listInfo.append(t)
        except:
            continue

def write(listInfo,num):
    print(len(listInfo))
    f=open(path,'w')
    f.close()
    with open(path,'a') as f:
        for i in range(num):
            f.write("{}\n".format(listInfo[i][0]))
        f.close()

if __name__=='__main__':
    listInfo=[]
    url="http://699pic.com/zhuanti/muqinjie2.html"
    html=getUrl(url)
    getphotos(html,listInfo)
    write(listInfo,10)
